import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store/index.js'
import LayOut from '@/views/LayOut.vue'
import Home from '@/views/Home/index.vue'
import TopCategory from '@/views/Category/index.vue'
import SubCategory from '@/views/Category/sub.vue'

import testPage from '@/views/test/test'

const GoodsPage = () => import('@/views/Goods/index')
const routes = [
  {
    path: '/',
    name: 'layout',
    component: LayOut,
    children: [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'home',
        component: Home,
        meta: { keepAlive: true }
      },
      {
        path: '/category/:id',
        name: 'TopCategory',
        component: TopCategory
      },
      {
        path: '/category/sub/:id',
        name: 'SubCategory',
        component: SubCategory,
        meta: { keepAlive: true }
      },
      {
        path: '/product/:id',
        name: 'ProductPage',
        component: GoodsPage
      },
      {
        path: '/test',
        name: 'testPage',
        component: testPage
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  scrollBehavior (to, from, savePostion) {
    // vue2.0  x  y  控制
    // vue3.0  left  top 控制
    // console.log(to, from, savePostion);
    // console.log(savePostion);
    if (savePostion) return savePostion
    return { left: 0, top: 0 }
  }
})

router.beforeEach((to, from, next) => {
  if (to.path === '/home') {
    store.state.category.activeItem = { activeChild: {} }
  }
  next()
})
export default router
